<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>动态表格演示</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }

        .container {
            max-width: 900px;
            margin: 0 auto;
            background-color: #fff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
        }

        .data-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 30px;
        }

        .data-table th, .data-table td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        .data-table th {
            background-color: #f8f9fa;
            font-weight: bold;
            color: #333;
        }

        .data-table tr:hover {
            background-color: #f5f5f5;
        }

        .form-group {
            margin-bottom: 15px;
        }

        label {
            display: block;
            margin-bottom: 5px;
            color: #333;
            font-weight: bold;
        }

        input[type="text"], input[type="number"] {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }

        .btn-submit {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }

        .btn-submit:hover {
            background-color: #45a049;
        }

        .no-data {
            text-align: center;
            color: #888;
            padding: 20px;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>动态表格演示</h1>
    </div>

    <%-- 处理表单提交 --%>
    <%
        request.setCharacterEncoding("UTF-8");
        List<Map<String, String>> tableData = (List<Map<String, String>>) session.getAttribute("tableData");
        if (tableData == null) {
            tableData = new ArrayList<>();
            session.setAttribute("tableData", tableData);
        }
        String method = request.getMethod();
        if ("POST".equalsIgnoreCase(method)) {
            String name = request.getParameter("name");
            String age = request.getParameter("age");
            String job = request.getParameter("job");
            if (name != null && !name.trim().isEmpty() && age != null && !age.trim().isEmpty() && job != null && !job.trim().isEmpty()) {
                Map<String, String> row = new HashMap<>();
                row.put("name", name.trim());
                row.put("age", age.trim());
                row.put("job", job.trim());
                tableData.add(row);
            }
        }
    %>

    <table class="data-table">
        <tr>
            <th>姓名</th>
            <th>年龄</th>
            <th>职业</th>
        </tr>
        <% if (tableData.isEmpty()) { %>
        <tr>
            <td colspan="3" class="no-data">暂无数据，请添加！</td>
        </tr>
        <% } else { %>
        <% for (Map<String, String> row : tableData) { %>
        <tr>
            <td><%= row.get("name") %>
            </td>
            <td><%= row.get("age") %>
            </td>
            <td><%= row.get("job") %>
            </td>
        </tr>
        <% } %>
        <% } %>
    </table>

    <form method="post" autocomplete="off">
        <div class="form-group">
            <label for="name">姓名：</label>
            <input type="text" id="name" name="name" maxlength="20" required/>
        </div>
        <div class="form-group">
            <label for="age">年龄：</label>
            <input type="number" id="age" name="age" min="1" max="120" required/>
        </div>
        <div class="form-group">
            <label for="job">职业：</label>
            <input type="text" id="job" name="job" maxlength="30" required/>
        </div>
        <button type="submit" class="btn-submit">添加</button>
    </form>
</div>
</body>
</html>
